var API = API || {};
var icon = 'bed-icon-small.png';
var startX = 0;
var startY = 0;

if (typeof m !== 'undefined' && m !== '') {
    m = $.parseJSON(m);

    if (m.length === 1) {
        icon = 'bed-icon-active.png';
    }

    if (typeof m[0] !== 'undefined' && m[0] !== '') {
        startX = m[0].x;
        startY = m[0].y;
    }

}

API.Google = {
    map: "",
    bound: new google.maps.LatLngBounds(),
    infowidnow: new google.maps.InfoWindow({}),
    markers: new Array(),
    Data: {
        Page: 'HOME',
        startX: 0,
        startY: 0,
        icon: API.Data.base_url + 'assets/img/google/' + icon
    },
    MapOptions: {
        center: new google.maps.LatLng(startX, startY),
        zoom: 17,
        mapTypeId: google.maps.MapTypeId.SATELLITE,
        panControl: true,
        zoomControl: true,
        mapTypeControl: false,
        scaleControl: true,
        streetViewControl: false,
        overviewMapControl: false,
        scrollwheel: false
    },
    AddMarker: function(LatLng, title, ID) {

        marker = new google.maps.Marker({
            position: LatLng,
            map: API.Google.map,
            icon: API.Google.Data.icon,
            title: title,
            hotspotid: ID
        });
        API.Google.bound.extend(LatLng);
        API.Google.BindInfoWindow(marker); // add an event listener for this marker

        API.Google.markers.push(marker); // add marker to paralel array width link index

    },
    BindInfoWindow: function(marker) {

        google.maps.event.addListener(marker, 'click', function() {
            API.Google.OpenInfoWidow(marker);
        });
    },
    OpenInfoWidow: function(marker) {

        obj = $.grep(m, function(obj) {     // find object in m array and return it
            return obj.ID === marker.hotspotid;
        });
        html = API.Google.BuildInfoHTML(obj[0]); // grep reutn found object in Array

        API.Google.infowidnow.setContent(html);
        API.Google.infowidnow.open(API.Google.map, marker);
    },
    BuildInfoHTML: function(h) {

        var lnk = '<a href="' + h.link + '">';
        var lnk_i = '<a class="image" href="' + h.link + '">';
        var HTML = '<div class="infowidnow">';

        // Make review if there is any
        if (h.reviews > 0) {
            HTML += '<div class="reviews">';
            HTML += '<div class="score">' + h.score + '</div>';
            HTML += '<div class="no_reviews">Score from<br><strong class="count">' + h.reviews + '</strong> review</div>';
            HTML += '</div>';
        }

        HTML += lnk_i + '<img src="' + h.image + '"></a>';
        HTML += '<h3>' + lnk + h.name + '</a></h1>';
        HTML += '<p>' + h.address + '</p>';
        return HTML + '</div>';
    },
    FindMarkerIndex: function(hotel_ID) {

        var m_index;

        $.each(m, function(index, e) {

            if (parseInt(e.ID) === parseInt(hotel_ID)) {
                m_index = index;
            }
        });

        return m_index;
    },
    Init: function(openID) {

        API.Google.markers = new Array();
        API.Google.bound = new google.maps.LatLngBounds();
        API.Google.infowidnow = new google.maps.InfoWindow({});

        API.Google.map = new google.maps.Map(document.getElementById("google_map"), API.Google.MapOptions);

        $.each(m, function(index, e) {

            var latLang = new google.maps.LatLng(e.x, e.y);
            API.Google.AddMarker(latLang, e.name, e.ID);
        });
        // Centritaj i zumiraj mapu ako ima vise obejkata
        if (m.length > 1) {
            API.Google.map.fitBounds(API.Google.bound);
        }

        $('.GH').on('click', function() {
            API.Google.OpenInfoWidow(API.Google.markers[$(this).attr('data-id')]); // Bind Links With Info Window
        });
        if (m.length === 1) {
            window.setTimeout(function() {
                API.Google.OpenInfoWidow(API.Google.markers[0]);
            }, 500);
        }

        if (openID !== "ALL") {
            index = API.Google.FindMarkerIndex(openID);

            window.setTimeout(function() {
                API.Google.OpenInfoWidow(API.Google.markers[index]);
            }, 500);
        }


    }
};

var element2 = document.createElement("script");
element2.src = API.Data.base_url + "assets/js/google_map.js";
document.body.appendChild(element2);